package com.shigu.admin.diy.services;

import com.opentae.data.mall.beans.DiyFunction;
import com.opentae.data.mall.beans.DiySql;
import com.opentae.data.mall.examples.DiyFunctionExample;
import com.opentae.data.mall.examples.DiySqlExample;
import com.opentae.data.mall.interfaces.DiyFunctionMapper;
import com.opentae.data.mall.interfaces.DiySqlMapper;
import com.shigu.admin.diy.bo.FuncAddBo;
import com.shigu.admin.diy.bo.FuncQueryBO;
import com.shigu.admin.diy.vo.DiyFuncVO;
import com.shigu.admin.manager.common.ShiguPager;
import com.shigu.component.utils.BeanMapper;
import com.shigu.exceptions.BaohuoException;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

/**
 *
 * 功能服务
 * Created by bugzy on 2017/7/11 0011.
 */
@Service
public class FunctionService {

    @Autowired
    private DiyFunctionMapper diyFunctionMapper;

    @Autowired
    private DiySqlMapper diySqlMapper;

    @Autowired
    private SqlService sqlService;

    public ShiguPager<DiyFuncVO> queryPages(FuncQueryBO bo) {
        ShiguPager<DiyFuncVO> pager = new ShiguPager<>();
        pager.setNumber(bo.getCurrentPage());
        DiyFunctionExample functionExample = new DiyFunctionExample();
        functionExample.setStartIndex((bo.getCurrentPage() - 1) * bo.getPageSize());
        functionExample.setEndIndex(bo.getPageSize());
        functionExample.setOrderByClause("function_id desc");

        DiyFunctionExample.Criteria criteria = functionExample.createCriteria();
        if (bo.getLocationType() != null) {
            criteria.andLocationTypeEqualTo(bo.getLocationType());
        }

        if (bo.getType() != null) {
            criteria.andTypeEqualTo(bo.getType());
        }

        if (bo.getPageId() != null) {
            criteria.andPageIdEqualTo(bo.getPageId());
        }

        int count = diyFunctionMapper.countByExample(functionExample);
        if (count > 0) {
            pager.calPages(count, bo.getPageSize());
            pager.setContent(BeanMapper.mapList(diyFunctionMapper.selectByConditionList(functionExample), DiyFuncVO.class));
        }
        return pager;
    }

    public void delById(Long funcId) {
        diyFunctionMapper.deleteByPrimaryKey(funcId);
    }

    @Transactional(rollbackFor = Exception.class)
    public void addFunc(FuncAddBo bo) throws BaohuoException {
        DiyFunction t = BeanMapper.map(bo, DiyFunction.class);
        try {
            if (StringUtils.isNotEmpty(bo.getRedirectTo())) {
                t.setRedirectTo(URLDecoder.decode(bo.getRedirectTo(), "UTF-8"));
            }
            if (StringUtils.isNotEmpty(bo.getJsScript())) {
                t.setJsScript(URLDecoder.decode(bo.getJsScript(), "UTF-8"));
            }
        } catch (UnsupportedEncodingException ignored) {
        }
        if (bo.getFunctionId() != null) {
            diyFunctionMapper.updateByPrimaryKeySelective(t);
        } else {
            diyFunctionMapper.insertSelective(t);
        }
        if (bo.getSqls() != null) {
            DiySqlExample sqlExample = new DiySqlExample();
            sqlExample.createCriteria().andTargetIdEqualTo(t.getFunctionId()).andUseForEqualTo(2);
            diySqlMapper.deleteByExample(sqlExample);
            for (String s : bo.getSqls()) {
                DiySql sql = new DiySql();
                sql.setSqlContent(s);
                sql.setUseFor(2);
                sql.setTargetId(t.getFunctionId());
                diySqlMapper.insertSelective(sql);
            }
        }
    }

    public DiyFuncVO queryById(Long id) {
        DiyFuncVO vo = BeanMapper.map(diyFunctionMapper.selectByPrimaryKey(id), DiyFuncVO.class);
        DiySql sql = new DiySql();
        sql.setUseFor(2);
        sql.setTargetId(id);
        vo.setSqls(sqlService.sqlsByUserfor(2, id));
        if (vo.getSqls().isEmpty()) {
            vo.getSqls().add("");
        }
        return vo;
    }
}
